<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <div>
                    <el-input v-model="query.keywords" placeholder="关键词" class="handle-input mr10"></el-input>
                    <el-date-picker class="ml10 mr10" v-model="query.date" type="daterange" unlink-panels
                        range-separator="到" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" />
                    <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </div>
                <div>
                    <el-button type="primary" size="small" icon="el-icon-plus" @click="handleCreate" class="add-btn">
                        添加医嘱</el-button>
                </div>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
                :cell-style="{padding:'3px 0'}">
                <el-table-column prop="advice_id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="inpatient_id" label="就诊号"></el-table-column>
                <el-table-column prop="send_opera_name" label="医生"></el-table-column>
                <el-table-column prop="hz_name" label="患者"></el-table-column>
                <el-table-column prop="hz_hop_no" label="住院号"></el-table-column>
                <el-table-column prop="advice_islong" label="期限">
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.advice_islong == 0" size="small">临时医嘱</el-tag>
                        <el-tag type="success" v-if="scope.row.advice_islong == 1" size="small">长期医嘱</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="advice_type" label="类型">
                    <template #default="scope">{{ getAdviceType(scope.row.advice_type) }}</template>
                </el-table-column>
                <el-table-column prop="advice_cxt" label="内容"></el-table-column>
                <el-table-column prop="advice_notes" label="嘱托"></el-table-column>
                <el-table-column prop="med_flag_grop" label="同组"></el-table-column>
                <el-table-column prop="advice_num" label="数量"></el-table-column>
                <el-table-column prop="advice_unit" label="单位"></el-table-column>
                <el-table-column prop="med_route" label="用法"></el-table-column>
                <el-table-column prop="med_specif" label="规格"></el-table-column>
                <el-table-column prop="med_dose_single" label="单次剂量"></el-table-column>
                <el-table-column prop="med_dose_unit" label="剂量单位"></el-table-column>
                <el-table-column prop="med_freq" label="频次"></el-table-column>

                <el-table-column prop="med_flag_self" label="自备">
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.med_flag_self == 0" size="small">普通</el-tag>
                        <el-tag type="success" v-if="scope.row.med_flag_self == 1" size="small">自备</el-tag>
                        <el-tag type="success" v-if="scope.row.med_flag_self == 2" size="small">PRN</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="send_time" label="时间"></el-table-column>



                <el-table-column label="操作" width="230" align="center">
                    <template #default="scope">
                        <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.page"
                    :page-size="query.limit" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog title="添加" v-model="addVisible" width="50%">
            <el-form label-width="110px">
                <el-form-item label="条形码">
                    <el-input v-model="createForm.advice_barcode"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input v-model="createForm.advice_cxt"></el-input>
                </el-form-item>
                <el-form-item label="嘱托">
                    <el-input v-model="createForm.advice_notes"></el-input>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="期限">
                            <el-radio-group v-model="createForm.advice_islong" class="ml-4">
                                <el-radio label="0" size="large">临时医嘱</el-radio>
                                <el-radio label="1" size="large">长期医嘱</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="类型">
                            <el-select v-model="createForm.advice_type" class="m-2" placeholder="请选择类型" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'advice_type')"
                                    :key="item.dic_vlu" :label="item.dic_vlu" :value="item.dic_id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="患者">
                            <el-select v-model="createForm.inpatient_id" class="m-2" placeholder="请选择患者" size="large">
                                <el-option v-for="item in patients" :key="item.inpatient_id" :label="item.hz_name"
                                    :value="item.inpatient_id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="执行">
                            <el-radio-group v-model="createForm.flag_imp" class="ml-4">
                                <el-radio label="0" size="large">无需</el-radio>
                                <el-radio label="1" size="large">立即</el-radio>
                                <el-radio label="2" size="large">PRN</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态">
                            <el-radio-group v-model="createForm.advice_state" class="ml-4">
                                <el-radio v-for="item in states" :label="item.id" size="large">{{item.title}}</el-radio>
                            </el-radio-group>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="人员">
                            <el-select v-model="createForm.opera_id" class="m-2" placeholder="请选择下达人员" size="large">
                                <el-option v-for="item in staffs" :key="item.staff_id" :label="item.staff_name"
                                    :value="item.staff_id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-row>
                    <el-col :span="12">
                        <el-form-item label="同组">
                            <el-input v-model="createForm.med_flag_grop"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="自备">
                            <el-radio-group v-model="createForm.med_flag_self" class="ml-4">
                                <el-radio label="0" size="large">普通</el-radio>
                                <el-radio label="1" size="large">自备</el-radio>
                                <el-radio label="2" size="large">PRN</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="数量">
                            <el-input v-model="createForm.advice_num" type="number"></el-input>
                            
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单位">
                            <el-select v-model="createForm.advice_unit" class="m-2" placeholder="请选择单位" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'advice_unit')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="用法">
                            <el-select v-model="createForm.med_route" class="m-2" placeholder="请选择用法" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'med_route')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="规格">
                            <el-input v-model="createForm.med_specif"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="单次剂量">
                            <el-input v-model="createForm.med_dose_single" type="number"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="剂量单位">
                            <el-select v-model="createForm.med_dose_unit" class="m-2" placeholder="请选择单位" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'med_dose_unit')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="频次">
                            <el-select v-model="createForm.med_freq" class="m-2" placeholder="请选择频次" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'med_freq')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="每天几次">
                            <el-input type="number" v-model="createForm.med_freq_conve"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="首天几次">
                            <el-input type="number" v-model="createForm.med_first_num"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="皮试">
                            <el-radio-group v-model="createForm.med_flag_skin" class="ml-4">
                                <el-radio label="0" size="large">不需</el-radio>
                                <el-radio label="1" size="large">需要</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="滴速">
                            <el-input v-model="createForm.med_drip_rate"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请单号">
                            <el-input v-model="createForm.request_no"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="addVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveCreate">确 定</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="70%">
            <el-form label-width="110px">
                <el-form-item label="条形码">
                    <el-input v-model="eidtForm.info.advice_barcode"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input v-model="eidtForm.info.advice_cxt"></el-input>
                </el-form-item>
                <el-form-item label="嘱托">
                    <el-input v-model="eidtForm.info.advice_notes"></el-input>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="期限">
                            <el-radio-group v-model="eidtForm.info.advice_islong" class="ml-4">
                                <el-radio label="0" size="large">临时医嘱</el-radio>
                                <el-radio label="1" size="large">长期医嘱</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="类型">
                            <el-select v-model="eidtForm.info.advice_type" class="m-2" placeholder="请选择类型" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'advice_type')"
                                    :key="item.dic_vlu" :label="item.dic_vlu" :value="item.dic_id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="患者">
                            <el-select v-model="eidtForm.info.inpatient_id" class="m-2" placeholder="请选择患者"
                                size="large">
                                <el-option v-for="item in patients" :key="item.inpatient_id" :label="item.hz_name"
                                    :value="item.inpatient_id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="执行">
                            <el-radio-group v-model="eidtForm.info.flag_imp" class="ml-4">
                                <el-radio label="0" size="large">无需</el-radio>
                                <el-radio label="1" size="large">立即</el-radio>
                                <el-radio label="2" size="large">PRN</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态">
                            <el-radio-group v-model="eidtForm.info.advice_state" class="ml-4">
                                <el-radio v-for="item in states" :label="item.id" size="large">{{item.title}}</el-radio>
                            </el-radio-group>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="人员">
                            <el-select v-model="eidtForm.info.opera_id" class="m-2" placeholder="请选择下达人员" size="large">
                                <el-option v-for="item in staffs" :key="item.staff_id" :label="item.staff_name"
                                    :value="item.staff_id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-row>
                    <el-col :span="12">
                        <el-form-item label="同组">
                            <el-input v-model="eidtForm.info.med_flag_grop"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="自备">
                            <el-radio-group v-model="eidtForm.info.med_flag_self" class="ml-4">
                                <el-radio label="0" size="large">普通</el-radio>
                                <el-radio label="1" size="large">自备</el-radio>
                                <el-radio label="2" size="large">PRN</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="数量">
                            <el-input v-model="eidtForm.info.advice_num" type="number"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单位">
                            <el-select v-model="eidtForm.info.advice_unit" class="m-2" placeholder="请选择单位" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'advice_unit')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>

                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="用法">
                            <el-select v-model="eidtForm.info.med_route" class="m-2" placeholder="请选择用法" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'med_route')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="规格">
                            <el-input v-model="eidtForm.info.med_specif"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="单次剂量">
                            <el-input v-model="eidtForm.info.med_dose_single" type="number"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="剂量单位">
                            <el-select v-model="eidtForm.info.med_dose_unit" class="m-2" placeholder="请选择单位"
                                size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'med_dose_unit')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="频次">
                            <el-select v-model="eidtForm.info.med_freq" class="m-2" placeholder="请选择频次" size="large">
                                <el-option v-for="item in adviceTypes.filter(v => v.father_key == 'med_freq')"
                                    :key="item.dic_id" :label="item.dic_vlu" :value="item.dic_vlu" />
                            </el-select>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="每天几次">
                            <el-input type="number" v-model="eidtForm.info.med_freq_conve"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="首天几次">
                            <el-input type="number" v-model="eidtForm.info.med_first_num"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="皮试">
                            <el-radio-group v-model="eidtForm.info.med_flag_skin" class="ml-4">
                                <el-radio label="0" size="large">不需</el-radio>
                                <el-radio label="1" size="large">需要</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="滴速">
                            <el-input v-model="eidtForm.info.med_drip_rate"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请单号">
                            <el-input v-model="eidtForm.info.request_no"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import { ref, reactive } from "vue";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { staffAll, adviceList, adviceCreate, adviceUpdate, patientAll, dictAdviceType } from "../api/index";
    import { typeName, log } from "../utils/user"

    export default {
        name: "basetable",
        setup() {
            const statusText = ["禁用", "启用"]
            const typeNames = typeName();

            let typeText = {};
            Object.keys(typeNames).forEach(k => {
                typeText[typeNames[k][0]] = typeNames[k][1]
            })
            log("typeText", typeText);

            const query = reactive({
                keywords: "",
                date: [],
                page: 1,
                limit: 15,
            });
            const adviceTypes = ref([])

            const tableData = ref([]);
            const pageTotal = ref(0);

            // 获取表格数据
            const getData = () => {
                adviceList(query).then((res) => {
                    if (res.status == 'ok') {
                        adviceTypes.value =  res.data.type
                        tableData.value = res.data.list.list;
                        pageTotal.value = res.data.list.pageTotal || 1;
                    } else {
                        ElMessage.error(res.msg)
                        return false;
                    }
                });
            };
            getData();

            // 查询操作
            const handleSearch = () => {
                query.page = 1;
                getData();
            };
            // 分页导航
            const handlePageChange = (val) => {
                query.page = val;
                getData();
            };

            // 删除操作
            const handleDelete = (index) => {
                // 二次确认删除
                ElMessageBox.confirm("确定要删除吗？", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        ElMessage.success("删除成功");
                        tableData.value.splice(index, 1);
                    })
                    .catch(() => { });
            };
            // 表格添加
            const addVisible = ref(false);
            const patients = ref([])
            const staffs = ref([])

            const handleCreate = () => {
                if (patients.value.length == 0) {
                    patientAll({}).then(res => {
                        if (res.status == 'ok') {
                            patients.value = res.data;
                            if (adviceTypes.value.length > 0 && staffs.value.length > 0) {
                                addVisible.value = true;
                            }
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                }
                if (adviceTypes.value.length == 0) {
                    dictAdviceType({}).then(res => {
                        if (res.status == 'ok') {
                            adviceTypes.value = res.data;
                            if (patients.value.length > 0 && staffs.value.length > 0) {
                                addVisible.value = true;
                            }
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                }
                if (staffs.value.length == 0) {
                    staffAll({}).then(res => {
                        if (res.status == 'ok') {
                            staffs.value = res.data;
                            if (adviceTypes.value.length > 0 && patients.value.length > 0) {
                                addVisible.value = true;
                            }
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                }

                if (patients.value.length > 0 && adviceTypes.value.length > 0 && staffs.value.length > 0) {
                    addVisible.value = true;
                }

            };
            const states = [
                {
                    id: "0",
                    title: "待审"
                },
                {
                    id: "1",
                    title: "审核"
                },
                {
                    id: "2",
                    title: "执行"
                },
                {
                    id: "3",
                    title: "作废"
                },
                {
                    id: "4",
                    title: "停止"
                },
            ]

            let createForm = reactive({
                advice_barcode: "",
                inpatient_id: "",
                advice_islong: "0",
                advice_type: "",
                advice_state: "0",
                advice_cxt: "",
                advice_notes: "",
                flag_imp: '1',
                opera_id: "",
                med_flag_grop: "",
                med_flag_self: "1",
                advice_num: "",
                advice_unit: "",
                med_route: "",
                med_specif: "",
                med_dose_single: "",
                med_dose_unit: "",
                med_freq: "",
                med_freq_conve: "",
                med_first_num: "",
                med_flag_skin: "0",
                med_drip_rate: "",
                request_no: "",
            });

            const saveCreate = () => {
                if (!createForm.advice_barcode) {
                    ElMessage.error('请输入条形码')
                    return false;
                }
                if (!createForm.inpatient_id) {
                    ElMessage.error('请选择患者')
                    return false;
                }

                adviceCreate(createForm).then(res => {
                    if (res.status == 'ok') {
                        addVisible.value = false;
                        ElMessage.success(res.msg);
                        query.pageIndex = 1;
                        getData();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })
            }


            // 表格编辑时弹窗和保存
            const editVisible = ref(false);
            let eidtForm = reactive({
                id: 0,
                info: {
                    advice_barcode: "",
                    inpatient_id: "",
                    advice_islong: "0",
                    advice_type: "",
                    advice_state: "0",
                    advice_cxt: "",
                    advice_notes: "",
                    flag_imp: '1',
                    opera_id: "",
                    med_flag_grop: "",
                    med_flag_self: "1",
                    advice_num: "",
                    advice_unit: "",
                    med_route: "",
                    med_specif: "",
                    med_dose_single: "",
                    med_dose_unit: "",
                    med_freq: "",
                    med_freq_conve: "",
                    med_first_num: "",
                    med_flag_skin: "0",
                    med_drip_rate: "",
                    request_no: "",
                }
            });
            let idx = -1;
            const handleEdit = (index, row) => {
                idx = index;
                eidtForm.info.advice_barcode = row["advice_barcode"];
                eidtForm.info.inpatient_id = row["inpatient_id"];
                eidtForm.info.advice_islong = row["advice_islong"];
                eidtForm.info.advice_type = parseInt(row["advice_type"]);
                eidtForm.info.advice_state = row["advice_state"];
                eidtForm.info.advice_cxt = row["advice_cxt"];
                eidtForm.info.advice_notes = row["advice_notes"];
                eidtForm.info.flag_imp = row["flag_imp"];

                eidtForm.info.opera_id = '';

                eidtForm.info.med_flag_grop = row["med_flag_grop"];
                eidtForm.info.med_flag_self = row["med_flag_self"];
                eidtForm.info.advice_num = row["advice_num"];
                eidtForm.info.advice_unit = row["advice_unit"];
                eidtForm.info.med_route = row["med_route"];
                eidtForm.info.med_specif = row["med_specif"];
                eidtForm.info.med_dose_single = row["med_dose_single"];
                eidtForm.info.med_dose_unit = row["med_dose_unit"];
                eidtForm.info.med_freq = row["med_freq"];
                eidtForm.info.med_freq_conve = row["med_freq_conve"];
                eidtForm.info.med_first_num = row["med_first_num"];
                eidtForm.info.med_flag_skin = row["med_flag_skin"];
                eidtForm.info.med_drip_rate = row["med_drip_rate"];
                eidtForm.info.request_no = row["request_no"];

                eidtForm.id = row["advice_id"];

                if (patients.value.length == 0) {
                    patientAll({}).then(res => {
                        if (res.status == 'ok') {
                            patients.value = res.data;
                            if (adviceTypes.value.length > 0 && staffs.value.length > 0) {
                                editVisible.value = true;
                            }
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                }
                if (adviceTypes.value.length == 0) {
                    dictAdviceType({}).then(res => {
                        if (res.status == 'ok') {
                            adviceTypes.value = res.data;
                            if (patients.value.length > 0 && staffs.value.length > 0) {
                                editVisible.value = true;
                            }
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                }
                if (staffs.value.length == 0) {
                    staffAll({}).then(res => {
                        if (res.status == 'ok') {
                            staffs.value = res.data;
                            if (adviceTypes.value.length > 0 && patients.value.length > 0) {
                                editVisible.value = true;
                            }
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                }

                if (patients.value.length > 0 && adviceTypes.value.length > 0 && staffs.value.length > 0) {
                    editVisible.value = true;
                }

            };
            const saveEdit = () => {
                if (!eidtForm.info.advice_barcode) {
                    ElMessage.error('请输入条形码')
                    return false;
                }
                adviceUpdate(eidtForm).then(res => {
                    if (res.status == 'ok') {
                        editVisible.value = false;
                        ElMessage.success(res.msg);
                        getData();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })

            };
            const toggleStatus = (id, status) => {
                log("toggleStatus", id)
                let temp = {
                    id: id,
                    info: {
                        status: status
                    }
                }

                ElMessageBox.confirm("确认修改吗", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        departUpdate(temp).then(res => {
                            if (res.status == 'ok') {
                                ElMessage.success(res.msg)
                                getData();
                                return false;
                            } else {
                                ElMessage.error(res.msg)
                                return false;
                            }
                        })
                    })
                    .catch(() => { });


            }
            const getShape = id => {
                let shape = shapes.find(v => v.id == id);
                return shape.title
            }

            const getAdviceType = id => {
                let advice = adviceTypes.value.find(v => v.dic_id == parseInt(id));
                return advice ? advice.dic_vlu : id
            }
            return {
                statusText,
                typeText,

                // data
                query,
                tableData,
                pageTotal,


                handleSearch,
                handlePageChange,
                handleDelete,

                editVisible,
                eidtForm,
                handleEdit,
                saveEdit,

                createForm,
                addVisible,
                handleCreate,
                saveCreate,

                patients,
                adviceTypes,
                getAdviceType,
                staffs,
                states,

                toggleStatus,
                getShape
            };
        },
    };
</script>

<style scoped>
    .handle-box {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .m-radio {
        margin: 12px;
    }
</style>